import moment from 'moment'
export function getFormatString(dateType) {
	let chartFormat = "",
		tableFormat = '';
	if (dateType == "day") {
		chartFormat = "HH:mm";
		tableFormat = "YYYY-MM-DD HH:mm";
	} else if (dateType == "week") {
		chartFormat = "MM-DD HH:mm";
		tableFormat = "YYYY-MM-DD HH:mm";
	} else if (dateType == "month") {
		chartFormat = "DD";
		tableFormat = "YYYY-MM-DD";
	} else if (dateType == "year") {
		chartFormat = "MM";
		tableFormat = "YYYY-MM";
	} else {
		chartFormat = "YYYY";
		tableFormat = "YYYY";
	}
	return [chartFormat, tableFormat]
}

export function getStartEndTimeStr(dateType, dateVal) {
	if (dateType == 'total') return ['', '']

	let format = 'YYYY-MM-DD HH:mm:ss'
	let startTimeStr = dateVal.format(format)
	let count = 1
	let unit = dateType + 's'
	if (dateType == 'week') {
		unit = 'days',
			count = 7
	}
	let endTimeStr = moment(dateVal).add(count, unit).format(format)
	return [startTimeStr, endTimeStr]
}

export function getDayMonthYear(dateType, dateVal) {
	let year = '',
		month = '',
		day = ''
	if (dateType == "day") {
		year = dateVal.year()
		month = dateVal.month() + 1
		day = dateVal.date()
	} else if (dateType == "week") {
		year = dateVal.year()
		month = dateVal.month() + 1
		day = dateVal.date()
	} else if (dateType == "month") {
		year = dateVal.year()
		month = dateVal.month() + 1
	} else if (dateType == "year") {
		year = dateVal.year()
	}
	return [year, month, day]
}


/**
 * 返回一个hj-date-picker返回的那种日期的值
 * @param {日期类型，可以是 day, week, month, year, total} dateType 
 * @param {moment类型的日期} value 
 */
export function getTheDateValue(dateType, value) {
	console.log('getTheDateValue', dateType, value)
	if (!value) {
		value = moment().startOf(dateType)
	}
	let format = getFormatString(dateType)
	let startEndTimeStr = getStartEndTimeStr(dateType, value)
	let yearMonthDays = getDayMonthYear(dateType, value)
	let res = {
		dateType,
		value,
		startTime: startEndTimeStr[0],
		endTime: startEndTimeStr[1],
		chartFormat: format[0],
		tableFormat: format[1],
		year: yearMonthDays[0],
		month: yearMonthDays[1],
		day: yearMonthDays[2],
	}
	console.log('getTheDateValue', res)
	return res
}
